//genera archivos kml
var i = 0; 
//iconos
arIcons=[
		{"name":"Default",
			"fname":"http://maps.google.com/mapfiles/kml/pal5/icon13.png",
			"shadow":"http://maps.google.com/mapfiles/kml/pal5/icon13s.png",
			"width":32,
			"height":32},
		{"name":"Hopedaje",
			"fname":"http://maps.google.com/mapfiles/kml/pal2/icon28.png",
			"shadow":"http://maps.google.com/mapfiles/kml/pal2/icon28s.png",
			"width":30,
			"height":30},
		{"name":"Restaurante",
			"fname":"http://maps.google.com/mapfiles/kml/pal2/icon43.png",
			"shadow":"http://maps.google.com/mapfiles/kml/pal2/icon43s.png",
			"width":30,
			"height":30},
		{"name":"Atractivo",
			"fname":"http://maps.google.com/mapfiles/kml/pal2/icon12.png",
			"shadow":"http://maps.google.com/mapfiles/kml/pal2/icon12s.png",
			"width":30,
			"height":30},
		{"name":"casa",
			"fname":"http://maps.google.com/mapfiles/kml/pal3/icon56.png",
			"shadow":"http://maps.google.com/mapfiles/kml/pal3/icon56s.png",
			"width":30,
			"height":30},
		{"name":"centros",
			"fname":"http://maps.google.com/mapfiles/kml/pal3/icon31.png",
			"shadow":"http://maps.google.com/mapfiles/kml/pal3/icon31s.png",
			"width":30,
			"height":30}	
		];
//los archivos .kml son almacenan informaación geoespaciasl que es interpretada por google earth

function KML(){
	//declaración de del cuerpo
	var body;
	//contructor
	this.Create=function()
	{
		this.body=$(document.createElement("kml"));
		this.body.attr("xmlns","http://www.opengis.net/kml/2.2");
	}
	
	//agrega una nueva marca
	this.addPlaceMark=function(nombre,descripcion,latitud,longitud,icono)
	{
		var _place=$(document.createElement("Placemark"));
		
		var _name=$(document.createElement("name"));		
		_name.html(nombre);
		
		var _description=$(document.createElement("description"));
		_description.html(descripcion);
		
		//crea un punto con las coordenadas
		var _point=$(document.createElement("Point"));		
		var _coordenadas=$(document.createElement("coordinates"));
		_coordenadas.html(longitud+","+latitud+",0");
		_point.append(_coordenadas);
	
		//estilo
		var _style=$(document.createElement("style"));
		var _iconstyle=$(document.createElement("IconStyle"));
		var _icon=$(document.createElement("icon"));	
		_icon.html(getIncon(icono));
		_iconstyle.append(_icon);
		_style.append(_iconstyle);	
		//agrega la información al place		
		_place.append(_name);
		_place.append(_description);
		_place.append(_point);
		_place.append(_style);	
		
		
		///
		placemark = ge.createPlacemark('');

  		var point = ge.createPoint('');
  		point.setLatitude(latitud);
  		point.setLongitude(longitud);		
  		placemark.setGeometry(point);
		
		placemark.setName(nombre)
		placemark.setDescription(descripcion)
		this.body.append(_place);
		points[cont]=[latitud,longitud];	
		htmls[cont]=descripcion;
		placemarks[cont]=placemark;
	//	ge.getFeatures().appendChild(placemark);
		 	
		$("#marca").append("<option value=\""+cont+"\" title=\""+cont+"\">"+nombre+"</option>");
		cont++;
	}
	this.text=function()
	{
		var str=this.body.html();
		str=str.replace(/placemark/gi,"Placemark");
		str=str.replace(/point/gi,"Point");
		str=str.replace(/icon/gi,"Icon");
		str=str.replace(/iconstyle/gi,"IconStyle");
		str=str.replace(/style/gi,"Style");
		  
		
		return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
				"<kml xmlns=\"http://www.opengis.net/kml/2.2\"> <Document>"+ str+ " </Document></kml>";
	}
	

}


//obtiene el icono correspondiente a un tema 
function getIncon(xtema)
{
	var n;
	switch(xtema)
	{
		//atractivos
		case 24:
			n=3;
		break;
		//hoteles
		case 4:
			n=1;
		break;
		//cabinas
		case 39:
			n=1;
		break;
		//Motel
		case 39:
			n=1;
		break;
		//otros centros 
		case 43:
			n=1;
		break;
		//complejos
		case 42:
			n=4;
		break;
		//albergues y posadas
		case 40:
			n=1;
		break;
		//pymes turismos
		case 54:
			n=5;
		break;
		//centros de desarrollo
		case 86:
			n=5;
		break;
		default:
		n=0;
	}
	//crea un nuevo tipo de icono	
	return arIcons[n].fname;
}